package com.swm.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.swm.util.ConditionUtilDC.getLines;
import static com.swm.util.ConditionUtilDC.getPrice;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData41 {

    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);

    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        JSONObject jsonObject = new JSONObject();

        for (Stock stock : stocks) {
            String code = stock.getCode();
            int type = stock.getType();
            jsonObject.put(code, type);

        }
        return stocks;
    }

    public static void main(String[] args) throws Exception {

        ExecutorService executorService = Executors.newFixedThreadPool(50);
        //获取所有股票列表
        List<Stock> stcokList = getStcokList();
        for (Stock stock : stcokList) {
            executorService.execute(() -> {
                try {
                    getData(stock.getCode(), stock.getType());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }

        // 关闭线程池，以优雅的方式

        // executorService.shutdown();
    }

    public static void getData(String code, int type) throws Exception {

        String data = getWebData(code, type);
        if (data == null) {
            return;
        }

        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get("data") != null) {
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            String name = jsonObject1.get("name").toString();
            JSONArray klines = JSONArray.parse(jsonObject1.get("klines").toString());
            if (klines.size() > 100) {

                //蛟龙出海战法
                int i = 1;
                //今日涨幅
                double v = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]);

                JSONObject lines = getLines(klines, i);
                double f1 = Double.parseDouble(lines.get("f1").toString());
                double te1 = Double.parseDouble(lines.get("te1").toString());
                double tw1 = Double.parseDouble(lines.get("tw1").toString());
                double th1 = Double.parseDouble(lines.get("th1").toString());
                double six1 = Double.parseDouble(lines.get("six1").toString());
                //开盘价
                double k = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[1]);
                //收盘价
                double s = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);

                //最高价
                double g = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[3]);
                boolean b0 = k < f1 && k < te1 && k < tw1;
                boolean bb = s > f1 && s > te1 && s > tw1;
                boolean b = k < f1 && k < te1 && k < tw1 && k < th1;
                boolean b1 = s > f1 && s > te1 && s > tw1 && s > th1;


                boolean bf = k < f1 && s > f1;
                boolean bte = k < te1 && s > te1;
                boolean btw = k < tw1 && s > tw1;
                boolean bth = k < th1 && s > th1;
                boolean bsix = k < six1 && s > six1;
                int flag = 0;
                if (bf) {
                    flag++;
                }
                if (bte) {
                    flag++;
                }
                if (btw) {
                    flag++;
                }
                if (bth) {
                    flag++;
                }
                if (bsix) {
                    flag++;
                }


                //今日成交额
                double v8 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[5]);
                //昨日成交额
                double v61 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[5]);
                //前日成交额
                double v62 = Double.parseDouble(klines.get(klines.size() - i - 2).toString().split(",")[5]);
                //大前日成交额
                double v66 = Double.parseDouble(klines.get(klines.size() - i - 3).toString().split(",")[5]);
                double v71 = v8 / v61;


                //近120天价格处于低位
                boolean price = getPrice(klines, i);
                //近120天成交量处于高位
                // boolean money = getMoney(klines, i);
                //近几日是否放量
                //boolean moneyUp = getMoneyUp(klines, 2, i);
                //收盘与最高差距
                double v0000 = (g - s) * 100 / k;
                //if (flag >1&&flag<4 &&v0000<0.1&& v <9.8 && v >0 &&price) {
                //if (v0000<0.1&& v <9.8 && v >0 &&price) {
                //if (flag >1&&flag<4 &&v0000<0.2&& v <9.8 && v >0 &&price) {
                //if (v0000<0.2&& v <9.8 && v >0 &&price) {
                // System.out.println("穿线数："+flag);
                //if(price && v < 9.8 && v >0&&v0000<0.2&& flag >1&&flag<4){
                //if(/*v0000<0.2&&*/ /*money&&*/(v0000<0.2||(flag >1&&flag<4))&& price && v < 9.8 && v >0){
                //if(v0000<0.2&&flag >1&&flag<4&& price && v < 9.8 && v >0){

                if ((v0000 < 0.2 || (flag > 1/*&&flag<4*/)) && price && v < 9.8 && v > 0) {
                    System.out.println(name + " 今日涨幅：" + v);
                }
            }
        }
    }


    public static String getWebData(String code, int type) {
        String data = null;
        try {
            //百度股票接口
            //https://finance.pae.baidu.com/vapi/v1/getquotation?group=quotation_kline_ab&code=000876&name=%E6%96%B0%E5%B8%8C%E6%9C%9B&ktype=day
            // 连接到东方财富接口
            String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f116,f117&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=" + type + "." + code + "&klt=101&fqt=1";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie", "qgqp_b_id=488de58836c5f30c189b9ad4c700b9f5; st_si=04137509447995; websitepoptg_api_time=1734056942833; st_asi=delete; HAList=ty-1-603999-%u8BFB%u8005%u4F20%u5A92%2Cty-0-002888-%u60E0%u5A01%u79D1%u6280%2Cty-0-002269-%u7F8E%u90A6%u670D%u9970%2Cty-0-000813-%u5FB7%u5C55%u5065%u5EB7%2Cty-0-002045-%u56FD%u5149%u7535%u5668%2Cty-0-300059-%u4E1C%u65B9%u8D22%u5BCC%2Cty-1-688605-N%u5148%u950B%2Cty-1-000001-%u4E0A%u8BC1%u6307%u6570; st_pvi=62081555482493; st_sp=2024-12-11%2010%3A26%3A42; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=60; st_psi=20241213164756225-113200301201-5528754653");
            // 设置连接超时时间为 10 秒
            con.setConnectTimeout(10000);
            // 设置读取超时时间为 10 秒
            con.setReadTimeout(10000);
            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            con.disconnect();
            data = response.toString();
        } catch (Exception e) {
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }

        return data;
    }
}

